home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
LANG
/
GOFER
/
scripts
/
recur
< prev
next >
Wrap
Text File
|
1993-11-25
|
1KB
|
45 lines
-- Fibonacci series modulo p GCW 10/11/92
-- recur n takes the prefix of an infinite list up to the point where
-- the first n items reoccur. This might not terminate!
recur :: Eq [a] => Int -> [a] -> [a]
-- recur n has to be defined on lists of items in an equality type, else
-- how can we test for recurrence?
recur n xs = ms ++ f (drop (length ms) xs) ms where
ms = take n xs
f (ys@(u:us)) zs | zs == take n ys = []
| otherwise = u:f us zs
-- NB. drop n drops n items from head of a list. take n drops all
-- the rest. The auxiliary function f is defined so that f xs ms is
-- the largest prefix of xs whose removal leaves a list with ms as
-- a prefix. ys@(u:us) means that the list ys has the form u:us.
-- The Lucas sequence starting with a,b.
lucas :: Int -> Int -> [Int]
lucas a b = a:lucas b (a+b)
-- The Fibonacci sequence.
fibs :: [Int]
fibs = lucas 0 1
-- what is cycle length of Fibonacci numbers modulo p ?
-- named after C.T.C.Wall.
wall :: Int -> Int
wall p = length (recur 2 [ x `mod` p | x <- fibs ])
-- [ wall n | n <- [2, 3, 5, 7, 11, 13] ] gives the values
-- [3, 8, 20, 16, 10, 28]
length :: [a] -> Int
length x = sum [ 1 | _ <- x ]
drop :: Int -> [a] -> [a]
drop 0 xs = xs
drop _ [] = []
drop (n+1) (x:xs) = drop n xs